package com.qiantu.api.entity;

import com.qiantu.api.BuildConfig;
import com.qiantu.api.db.converter.DeviceBindingDataPropertyConverter;
import com.qiantu.api.db.converter.DeviceCustomKeyPropertyConverter;
import com.qiantu.api.db.converter.DeviceStatePropertyConverter;
import com.qiantu.api.db.converter.DeviceTriggerTypePropertyConverter;
import com.qiantu.api.utils.ThemeUtil;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Transient;
import org.greenrobot.greendao.annotation.Unique;

import java.util.List;

@Entity
public class DeviceBean {
    public static final int ENVIRONMENT_TYPE_TEMPERATURE = 2;
    public static final int ENVIRONMENT_TYPE_HUMIDITY = 3;
    public static final int ENVIRONMENT_TYPE_AIR_QUALITY = 5;
    public static final int ENVIRONMENT_TYPE_HUMAN_BODY_INDUCTION = 9;
    public static final int ENVIRONMENT_TYPE_HUMAN_PRESENCE_INDUCTION = 10;

    @Id(autoincrement = true)
    private Long localId;
    @Unique
    private String deviceSerialNo;
    private String deviceGroupSerialNo;
    private String controllerSerialNo;
    //给壳子用的
    private String deviceCollectSerialNo;
    //房屋下排序
    private int sort;
    //房屋下保存上一次的顺序
    private int lastSort;
    private int loadSort;
    //房间下排序
    private int roomSort;
    private int floorSort;
    private int floorGlobalSort;
    //本地字段，根据配置文件赋值
    private String type;
    private String deviceType;
    private String deviceMac;
    private String deviceSn;
    private String name;
    @Convert(converter = DeviceStatePropertyConverter.class, columnType = String.class)
    private DeviceState stateData;
    private boolean isFavourite;
    private boolean isOnline;
    private boolean isOpen;
    //状态类型：0离线，1在线，2未知，3网络异常
    private int stateType;
    private String imageUrl;
    private boolean isDisable;
    //是否展开
    private boolean isSwitch;
    //设备路数和状态数据路数相同
    private int pos;
    private String productModel;
    //房屋序列号 当与房屋绑定时，该值有效
    private String houseSerialNo;
    private String houseName;
    // 默认 : 0
    // 与房间绑定 : 1
    // 通用 : 2
    // 与用户绑定 : 3
    private int linkType;
    @NotNull
    private String roomSerialNo;
    private String roomName;
    private String floorSerialNo;
    private String floorName;
    private boolean isHomePage;
    private String version;
    private String ip;

    //是否是触发类设备  0否 1是
    private int trigger;
    //是否是动作类设备  0否 1是
    private int isMission;
    //是否是环境类设备 0否 1是
    private int isEnvironment;
    /**
     * 环境类型
     * 定时 : 1 ,
     * 环境温度 : 2 ,
     * 环境湿度 : 3 ,
     * 环境天气 : 4 ,
     * 环境空气质量 : 5 ,
     * 环境日出日落 : 6 ,
     * 定位 : 7 ,
     * 设备状态 : 8 ,
     * 人体感应 : 9 ,
     * 人体存在感应 : 10
     */
    private int environmentType;
    //首页设备项是否有展开 0否 1是
    private int hasCard;
    //是否有控制卡片
    private int hasControlCard;
    //首页设备项是否有开关按钮 0有 1没有
    private String mainControlType;

    @Transient
    @Convert(converter = DeviceTriggerTypePropertyConverter.class, columnType = String.class)
    private List<DeviceTriggerType> triggerData;

    @Transient
    @Convert(converter = DeviceBindingDataPropertyConverter.class, columnType = String.class)
    private List<DeviceBindingData> bingingData;
    @Transient
    private List<DeviceCustomKeyBean> deviceCustomKeys;
    //首页设备项是否有开关按钮 0有 1没有
    private int deviceGroup;

    //为了首页统一数据添加的临时字段
    @Transient
    private List<SceneBean> homeScenes;
    @Transient
    private List<DeviceBindGroupBean> deviceBindGroups;
    @Transient
    private List<DeviceBean> homeEnviroments;
    @Transient
    private SourceHomepageBean homepage;
    @Transient
    private Object temporary;

    @Generated(hash = 1874311414)
    public DeviceBean(Long localId, String deviceSerialNo, String deviceGroupSerialNo, String controllerSerialNo, String deviceCollectSerialNo, int sort,
            int lastSort, int loadSort, int roomSort, int floorSort, int floorGlobalSort, String type, String deviceType, String deviceMac, String deviceSn,
            String name, DeviceState stateData, boolean isFavourite, boolean isOnline, boolean isOpen, int stateType, String imageUrl, boolean isDisable,
            boolean isSwitch, int pos, String productModel, String houseSerialNo, String houseName, int linkType, @NotNull String roomSerialNo, String roomName,
            String floorSerialNo, String floorName, boolean isHomePage, String version, String ip, int trigger, int isMission, int isEnvironment,
            int environmentType, int hasCard, int hasControlCard, String mainControlType, int deviceGroup) {
        this.localId = localId;
        this.deviceSerialNo = deviceSerialNo;
        this.deviceGroupSerialNo = deviceGroupSerialNo;
        this.controllerSerialNo = controllerSerialNo;
        this.deviceCollectSerialNo = deviceCollectSerialNo;
        this.sort = sort;
        this.lastSort = lastSort;
        this.loadSort = loadSort;
        this.roomSort = roomSort;
        this.floorSort = floorSort;
        this.floorGlobalSort = floorGlobalSort;
        this.type = type;
        this.deviceType = deviceType;
        this.deviceMac = deviceMac;
        this.deviceSn = deviceSn;
        this.name = name;
        this.stateData = stateData;
        this.isFavourite = isFavourite;
        this.isOnline = isOnline;
        this.isOpen = isOpen;
        this.stateType = stateType;
        this.imageUrl = imageUrl;
        this.isDisable = isDisable;
        this.isSwitch = isSwitch;
        this.pos = pos;
        this.productModel = productModel;
        this.houseSerialNo = houseSerialNo;
        this.houseName = houseName;
        this.linkType = linkType;
        this.roomSerialNo = roomSerialNo;
        this.roomName = roomName;
        this.floorSerialNo = floorSerialNo;
        this.floorName = floorName;
        this.isHomePage = isHomePage;
        this.version = version;
        this.ip = ip;
        this.trigger = trigger;
        this.isMission = isMission;
        this.isEnvironment = isEnvironment;
        this.environmentType = environmentType;
        this.hasCard = hasCard;
        this.hasControlCard = hasControlCard;
        this.mainControlType = mainControlType;
        this.deviceGroup = deviceGroup;
    }

    @Generated(hash = 74682814)
    public DeviceBean() {
    }



    public Long getLocalId() {
        return localId;
    }

    public void setLocalId(Long localId) {
        this.localId = localId;
    }

    public String getDeviceSerialNo() {
        return deviceSerialNo;
    }

    public void setDeviceSerialNo(String deviceSerialNo) {
        this.deviceSerialNo = deviceSerialNo;
    }

    public String getControllerSerialNo() {
        return controllerSerialNo;
    }

    public void setControllerSerialNo(String controllerSerialNo) {
        this.controllerSerialNo = controllerSerialNo;
    }

    public int getSort() {
        return sort;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }

    public int getLastSort() {
        return lastSort;
    }

    public void setLastSort(int lastSort) {
        this.lastSort = lastSort;
    }

    public int getRoomSort() {
        return roomSort;
    }

    public void setRoomSort(int roomSort) {
        this.roomSort = roomSort;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public DeviceState getStateData() {
        return stateData;
    }

    public void setStateData(DeviceState stateData) {
        this.stateData = stateData;
    }

    public void setStatus(DeviceState.StatusDTO status) {
        setOnline(status.getOnline());
        setPower(status.getPower());
        stateData.setStatus(status);
    }

    public void setIsFavourite(boolean isFavourite) {
        this.isFavourite = isFavourite;
    }

    public void setIsOnline(boolean isOnline) {
        this.isOnline = isOnline;
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setOnline(isOnline ? 1 : 0);
        }
    }

    public void setIsSwitch(boolean isSwitch) {
        this.isSwitch = isSwitch;
    }

    public void setIsOpen(boolean isOpen) {
        this.isOpen = isOpen;
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setPower(isOpen ? 1 : 0);
        }
    }

    public int getStateType() {
        return stateType;
    }

    public void setStateType(int stateType) {
        this.stateType = stateType;
    }

//    public String getStatusImageUrl() {
//        return imageUrl;
//    }

    public String getStatusImageUrl() {
        return BuildConfig.APP_RESOURCE_BASE + ThemeUtil.getIconThemeSuffix() + imageUrl + (getIsOpen() || "1".equals(mainControlType) ? "_on.png" : "_off.png");
    }

    public String getOnImageUrl() {
        return BuildConfig.APP_RESOURCE_BASE + ThemeUtil.getIconThemeSuffix() + imageUrl + "_on.png";
    }

    public String getOffImageUrl() {
        return BuildConfig.APP_RESOURCE_BASE + ThemeUtil.getIconThemeSuffix() + imageUrl + "_off.png";
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public void setIsDisable(boolean isDisable) {
        this.isDisable = isDisable;
    }

    public String getHouseSerialNo() {
        return houseSerialNo;
    }

    public void setHouseSerialNo(String houseSerialNo) {
        this.houseSerialNo = houseSerialNo;
    }

    public String getHouseName() {
        return houseName;
    }

    public void setHouseName(String houseName) {
        this.houseName = houseName;
    }

    public String getRoomSerialNo() {
        return roomSerialNo;
    }

    public void setRoomSerialNo(String roomSerialNo) {
        this.roomSerialNo = roomSerialNo;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }

    public String getFloorName() {
        return floorName;
    }

    public void setFloorName(String floorName) {
        this.floorName = floorName;
    }

    public int getLinkType() {
        return linkType;
    }

    public void setLinkType(int linkType) {
        this.linkType = linkType;
    }

    public int getTrigger() {
        return trigger;
    }

    public void setTrigger(int trigger) {
        this.trigger = trigger;
    }

    public int getIsMission() {
        return isMission;
    }

    public void setIsMission(int isMission) {
        this.isMission = isMission;
    }

    public int getIsEnvironment() {
        return isEnvironment;
    }

    public void setIsEnvironment(int isEnvironment) {
        this.isEnvironment = isEnvironment;
    }

    public int getEnvironmentType() {
        return environmentType;
    }

    public void setEnvironmentType(int environmentType) {
        this.environmentType = environmentType;
    }

    public int getHasCard() {
        return hasCard;
    }

    public void setHasCard(int hasCard) {
        this.hasCard = hasCard;
    }

    public int getHasControlCard() {
        return hasControlCard;
    }

    public void setHasControlCard(int hasControlCard) {
        this.hasControlCard = hasControlCard;
    }

    public String getMainControlType() {
        return mainControlType != null ? mainControlType : "";
    }

    public void setMainControlType(String mainControlType) {
        this.mainControlType = mainControlType;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public List<DeviceTriggerType> getTriggerData() {
        return triggerData;
    }

    public void setTriggerData(List<DeviceTriggerType> triggerData) {
        this.triggerData = triggerData;
    }

    public List<DeviceBindingData> getBingingData() {
        return bingingData;
    }

    public void setBingingData(List<DeviceBindingData> bingingData) {
        this.bingingData = bingingData;
    }

    public List<DeviceCustomKeyBean> getDeviceCustomKeys() {
        return deviceCustomKeys;
    }

    public void setDeviceCustomKeys(List<DeviceCustomKeyBean> deviceCustomKeys) {
        this.deviceCustomKeys = deviceCustomKeys;
    }

    public void setDeviceBaseConfigInfo(DeviceBaseConfigInfo deviceBaseConfigInfo) {
        if (deviceBaseConfigInfo != null) {
            setType(deviceBaseConfigInfo.getType());
            setTrigger(deviceBaseConfigInfo.getTrigger());
            setIsMission(deviceBaseConfigInfo.getIsMission());
            setIsEnvironment(deviceBaseConfigInfo.getIsEnvironment());
            setEnvironmentType(deviceBaseConfigInfo.getEnvironmentType());
            setHasCard(deviceBaseConfigInfo.getHasCard());
            setHasControlCard(deviceBaseConfigInfo.getHasControlCard());
            setMainControlType(deviceBaseConfigInfo.getMainControlType());
            setTriggerData(deviceBaseConfigInfo.getTriggerData());
            setBingingData(deviceBaseConfigInfo.getBingingData());
        }
    }

    public Integer getLight() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getLight();
    }

    public void setLight(Integer light) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setLight(light);
        }
    }

    public Integer getRed() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getRed();
    }

    public void setRed(Integer red) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setRed(red);
        }
    }

    public Integer getGreen() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getGreen();
    }

    public void setGreen(Integer green) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setGreen(green);
        }
    }


    public Integer getBlue() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getBlue();
    }

    public void setBlue(Integer blue) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setBlue(blue);
        }
    }

    public Integer getStrokeState() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getStrokestate();
    }

    public void setStrokeState(Integer strokeState) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setStrokestate(strokeState);
        }
    }

    public Integer getStrokePercentage() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getStrokepercentage();
    }

    public void setStrokeStatePercentage(Integer strokeStatePercentage) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setStrokepercentage(strokeStatePercentage);
        }
    }

    public Integer getKelvin() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getKelvin();
    }

    public void setKelvin(Integer kelvin) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setKelvin(kelvin);
        }
    }

    public Integer getMode() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getMode();
    }

    public void setMode(Integer mode) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setMode(mode);
        }
    }

    public Integer getWindSpeed() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getWindspeed();
    }

    public void setWindSpeed(Integer windSpeed) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setWindspeed(windSpeed);
        }
    }
    public int getLock(){
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getLock() == null ? 0 : getStateData().getStatus().getLock();
    }
    public void setLock(Integer lock){
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setLock(lock);
        }
    }


    public Integer getExhaustSpeed() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getExhaustspeed();
    }

    public void setExhaustSpeed(Integer exhaustSpeed) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setExhaustspeed(exhaustSpeed);
        }
    }

    public Integer getWindDirection() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getWinddirection();
    }

    public void setWindDirection(Integer windDirection) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setWinddirection(windDirection);
        }
    }

    public Integer getTemperature() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getTemperature();
    }

    public void setTemperature(Integer temperature) {
        if (getStateData() != null && getStateData().getStatus() != null) {
            getStateData().getStatus().setTemperature(temperature);
        }
    }

    public Integer getAmbienttemperature() {
        if (getStateData() == null) {
            return 0;
        }
        if (getStateData().getStatus() == null) {
            return 0;
        }
        return getStateData().getStatus().getAmbienttemperature();
    }

    public void setSensitivity(Integer sensitivity) {
        if (getStateData() != null && getStateData().getSettings() != null) {
            getStateData().getSettings().setSensitivity(sensitivity);
        }
    }

    public Integer getSensitivity() {
        if (getStateData() == null) {
            return null;
        }
        if (getStateData().getSettings() == null) {
            return null;
        }
        return getStateData().getSettings().getSensitivity();
    }

    public void setPeriod(Float peroid) {
        if (getStateData() != null && getStateData().getSettings() != null) {
            getStateData().getSettings().setPeriod(peroid);
        }
    }

    public Float getPeriod() {
        if (getStateData() == null) {
            return null;
        }
        if (getStateData().getSettings() == null) {
            return null;
        }
        return getStateData().getSettings().getPeriod();
    }

    public String getType() {
        return type != null ? type : "";
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }


    public String getDeviceMac() {
        return deviceMac;
    }

    public void setDeviceMac(String deviceMac) {
        this.deviceMac = deviceMac;
    }

    public String getDeviceSn() {
        return deviceSn;
    }

    public void setDeviceSn(String deviceSn) {
        this.deviceSn = deviceSn;
    }

    public int getPos() {
        return pos;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }

    public String getProductModel() {
        return productModel;
    }

    public void setProductModel(String productModel) {
        this.productModel = productModel;
    }

    public void setPower(int power) {
        if (power == 1) {
            setIsOpen(true);
        } else {
            setIsOpen(false);
        }
    }

    public void setOnline(int online) {
        if (online == 1) {
            setIsOnline(true);
        } else {
            setIsOnline(false);
        }
    }

    public boolean getIsFavourite() {
        return this.isFavourite;
    }

    public boolean getIsOnline() {
        if(getDeviceGroup() == 1){
            return true;
        }
        if (getStateData() != null && getStateData().getStatus() != null && getStateData().getStatus().getOnline() != -1){
            return getStateData().getStatus().getOnline() != 0 && stateType == 1;
        }
        return stateType == 1;
    }

    public boolean getIsOpen() {
        if (getStateData() != null && getStateData().getStatus() != null) {
            this.isOpen = getStateData().getStatus().getPower() == 1;
        }
        return this.isOpen;
    }

    public boolean getIsDisable() {
        return this.isDisable;
    }

    public boolean getIsSwitch() {
        return this.isSwitch;
    }

    public String getImageUrl() {
        return this.imageUrl;
    }

    public boolean getIsHomePage() {
        return isHomePage;
    }

    public void setIsHomePage(boolean isHomePage) {
        this.isHomePage = isHomePage;
    }

    public List<SceneBean> getHomeScenes() {
        return homeScenes;
    }

    public void setHomeScenes(List<SceneBean> homeScenes) {
        this.homeScenes = homeScenes;
    }

    public List<DeviceBean> getHomeEnviroments() {
        return homeEnviroments;
    }

    public void setHomeEnviroments(List<DeviceBean> homeEnviroments) {
        this.homeEnviroments = homeEnviroments;
    }

    public Object getTemporary() {
        return temporary;
    }

    public void setTemporary(Object temporary) {
        this.temporary = temporary;
    }

    public List<DeviceBindGroupBean> getDeviceBindGroups() {
        return deviceBindGroups;
    }

    public void setDeviceBindGroupBeans(List<DeviceBindGroupBean> deviceBindGroups) {
        this.deviceBindGroups = deviceBindGroups;
    }

    public int getDeviceGroup() {
        return this.deviceGroup;
    }

    public void setDeviceGroup(int deviceGroup) {
        this.deviceGroup = deviceGroup;
    }

    public String getDeviceGroupSerialNo() {
        return this.deviceGroupSerialNo;
    }

    public void setDeviceGroupSerialNo(String deviceGroupSerialNo) {
        this.deviceGroupSerialNo = deviceGroupSerialNo;
    }

    public SourceHomepageBean getHomepage() {
        return this.homepage;
    }

    public void setHomePage(SourceHomepageBean homepage) {
        this.homepage = homepage;
    }

    public int getLoadSort() {
        return this.loadSort;
    }

    public void setLoadSort(int loadSort) {
        this.loadSort = loadSort;
    }

    public int getFloorSort() {
        return this.floorSort;
    }

    public void setFloorSort(int floorSort) {
        this.floorSort = floorSort;
    }

    public int getFloorGlobalSort() {
        return this.floorGlobalSort;
    }

    public void setFloorGlobalSort(int floorGlobalSort) {
        this.floorGlobalSort = floorGlobalSort;
    }

    public String getFloorSerialNo() {
        return this.floorSerialNo;
    }

    public void setFloorSerialNo(String floorSerialNo) {
        this.floorSerialNo = floorSerialNo;
    }

    public String getDeviceCollectSerialNo() {
        return this.deviceCollectSerialNo;
    }

    public void setDeviceCollectSerialNo(String deviceCollectSerialNo) {
        this.deviceCollectSerialNo = deviceCollectSerialNo;
    }
}

